package com.example.democontactperson;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ContactView<T> extends View {
    public static final int INVALID_POSITION = -1;
    private final Paint mFontPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ;
    private final Rect mTextBounds = new Rect();
    private final Paint mPaintRect = new Paint();
    ;
    private int mTextColor = 0xffF5BA23;
    private int mSelectBackGroundColor = Color.GRAY;
    private int mTextSize = 25;
    private T[] mLetters;

    private Rect[] mRectArray;

    private OnScrollTouchListener mOnScrollTouchListener;

    public ContactView(Context context) {
        this(context, null, 0);
    }

    public ContactView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ContactView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mFontPaint.setFakeBoldText(true);
        mFontPaint.setAntiAlias(true);
        mFontPaint.setColor(mTextColor); //
        mFontPaint.setTextSize(mTextSize);
        mPaintRect.setColor(mSelectBackGroundColor);
        // init();
    }



    private int mCount;
    private int mOldCount;
    private float mFontHeightSpace;
    private int mSelectPosition = INVALID_POSITION;


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = mCount;
        final int paddingTo = getPaddingTop();
        final int paddintBo = getPaddingBottom();
        final float height = getMeasuredHeight() - paddingTo - paddintBo;
        mFontHeightSpace = count == 0 ? 0 : height / count;
        if (count > 0) {
            final int oldCount = mOldCount;
            Rect[] rects = mRectArray;

            if (rects == null || oldCount != count) {
                rects = new Rect[count];
            }
            fullRect(rects, paddingTo, paddintBo, mFontHeightSpace);
            mRectArray = rects;

        }
    }

    private void fullRect(Rect[] rects, int paddingTop, int paddingBottom, float fontHeightSpace) {
        for (int i = 0; i < rects.length; i++) {
            Rect r = rects[i];
            if (r == null) {
                r = new Rect();
            }
            r.set(0, (int) ((i) * mFontHeightSpace) + paddingTop,
                    getMeasuredWidth(), (int) ((i + 1) * mFontHeightSpace)
                            + paddingBottom);
            rects[i] = r;
        }

    }


    @Override
    protected void onDraw(Canvas canvas) {

        if (mCount > 0) {
            if (mSelectPosition > INVALID_POSITION && mSelectPosition < mCount) {
                final Rect selRect = mRectArray[mSelectPosition];
                mPaintRect.setColor(mSelectBackGroundColor);
                canvas.drawRect(selRect, mPaintRect);
            }

            final FontMetrics fontMetrics = new FontMetrics();
            for (int i = 0; i < mCount; i++) {
                final String c = mLetters[i].toString();
                mFontPaint.getTextBounds(c, 0, c.length(), mTextBounds);

                mFontPaint.getFontMetrics(fontMetrics);

                // float f = mPaint.getFontSpacing();
                // float ff = -mFontMetrics.top;
                final int fontHeight = (int) (fontMetrics.bottom - fontMetrics.top);

                final Rect selRect = mRectArray[i];
                final int fontX = (int) ((selRect.width() - mTextBounds.width()) * 0.5);
                final int offectY = (int) ((selRect.height() - fontHeight) * 0.5);
                final int fontY = (int) (selRect.top - fontMetrics.top + offectY);
                canvas.drawText(c, fontX, fontY, mFontPaint);
            }
        }

    }

//    private Rect mSelectRect;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mCount <= 0)
            return false;

        final int actionMasked = event.getActionMasked();
        switch (actionMasked & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE: {

                final float y = (int) event.getY();
                if (y < getPaddingTop())
                    return true;
                if (y > getHeight() - getPaddingBottom())
                    return true;


                int position = getSelectPosition(y);

                if (position != mSelectPosition) {
                    mSelectPosition = position;
                    if (mOnScrollTouchListener != null) {
                        mOnScrollTouchListener.onScroll(this, position, mCount,
                                mLetters[position]);
                    }
                    invalidate();
                }
                break;
            }
        }


        return true;
    }


    public void setTextColor(int color) {
        mTextColor = color;
        mFontPaint.setColor(mTextColor); //
        invalidate();
    }

    public void setTextSize(int textSize) {
        mTextSize = textSize;
        mFontPaint.setTextSize(mTextSize);

        invalidate();
    }

    public void setSelectBackGroundColor(int color) {
        mSelectBackGroundColor = color;
        mPaintRect.setColor(mSelectBackGroundColor);
        invalidate();
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void setSelectPosition(int position) {
        if (mCount > 0 && mSelectPosition != position) {
            mSelectPosition = position;
            invalidate();
        }
    }


    int getSelectPosition(float y) {
        final float startY = y - getPaddingTop();
        int position = (int) (startY / mFontHeightSpace);
        position = position < 0 ? 0 : position >= mCount ? mCount - 1 : position;
        return position;
    }

//    void setSelectPosition(float x, float y) {
//        final int pos = getSelectPosition(x, y);
//        setSelectPosition(pos);
//    }

//    public void setSelectPosition(int position) {
//        mSelectPosition = position;
//        mSelectRect = getSelectRect(position);
//        invalidate();
//    }

    public void setOnScrollTouchListener(OnScrollTouchListener listener) {
        mOnScrollTouchListener = listener;
    }

    public interface OnScrollTouchListener<T> {
        void onScroll(ContactView view, int position, int count, T c);
    }

    // public void setLetters(List<String> letters) {
    // if (mLetters == letters)
    // return;
    // mLetters = letters;
    // requestLayout();
    // invalidate();
    // }

    public void setCharacters(T[] lps) {
        mOldCount = mCount;
        mCount = lps == null ? null : lps.length;
        mLetters = lps;
        mSelectPosition = INVALID_POSITION;
        requestLayout();
        invalidate();
    }

//    public void setSelectLetter(String letter) {
//        for (int i = 0; i < mLetters.length; i++) {
//            if (mLetters[i].equals(letter)) {
//                setSelectPosition(i);
//                break;
//            }
//        }
//
//    }


}
